<template>
  <div class='container'>
  </div>
</template>
<script>
import { userPermissionApi } from "@/services/UserPermission";
export default {
  data () {
    return {
      token: '',
      userId: '',
      userInfo: {}
    }
  },
  methods: {
    // 通过短token获得长token
    getAccessToken () {
      return userPermissionApi.getAccessToken({
        data: {
          short_token: this.token
        }
      }).then(res => {
        if (res.code === 0) {
          window.localStorage.clear()
          window.sessionStorage.clear()
          this.userId = res.data.user_id
          // window.sessionStorage.setItem('access_token', res.data.access_token)
          window.localStorage.setItem('access_token', res.data.access_token)
          window.sessionStorage.setItem('access_token', res.data.access_token)
        }
      })
    },
    // 获取用户详情
    getUserInfo () {
      return userPermissionApi.getUserInfo({
        data: {
          user_id: this.userId
        }
      }).then(res => {
        if (res.code === 0) {
          res.data.job_list = res.data.job_data
          let userInfo = res.data
          userInfo.short_token = this.token
          this.userInfo = userInfo
          window.localStorage.setItem("user_id", this.userId);
          // window.localStorage.setItem("userInfo", JSON.stringify(userInfo));
        }
      })
    },
    getUserDetail () {
      return userPermissionApi.getUserDetailByToken().then(res => {
        if (res.code === 0) {
          let data = res.data || {}
          this.userInfo = Object.assign(this.userInfo, data)
          window.localStorage.setItem("userInfo", JSON.stringify(this.userInfo))
        }
      })
    },
    // 获取用户权限
    getUserAuth () {
      return userPermissionApi.getUserAuth({
        data: {
          user_id: this.userId
        }
      }).then(res => {
        let userAuthList = res.data.list;
        if (userAuthList) {
          this.$store.dispatch("setUserAuth", res.data.list);
        }
      },
        err => {
          console.log(err);
        }
      );
    },
    // 根据域名获取单位信息
    getCompanyByDomain () {
      let domain =
        location.host.indexOf("localhost") >= 0
          ? "dev_platform.ihibuilding.cn"
          : location.host;
      // let domain = 'dev_platform.ihibuilding.cn'
      return userPermissionApi.getCompanyByDomain({
        data: {
          company_domain: domain
        }
      }).then(res => {
        window.localStorage.setItem(
          "companyInfo",
          JSON.stringify(res.data)
        );
        this.companyInfo = res.data;
        this.containerStyle = {
          backgroundImage: `url(${this.companyInfo.image_picture})`
        }
        // 设置页面title为接口返回的数据
        let title = document.getElementsByTagName("title");
        title[0].innerText = this.companyInfo.platform_name || this.companyInfo.company_name;
        // 如果设置了公司logo，就将浏览器标签页的icon修改为logo
        if (this.companyInfo.company_logo) {
          let logo = document.querySelector(".tab-icon");
          logo.href = this.companyInfo.company_logo;
        }
      }, err => {
        console.log(err);
      }
      );
    },
  },
  created () {
  },
  async mounted () {
    this.token = this.$route.query.tokenId
    let redirectPath = this.$route.query.path
    let projectId = this.$route.query.project_id
    // 记录跳转前的网址，登出时返回原平台
    let originUrl = this.$route.query.url
    if (originUrl) {
      window.localStorage.setItem('originUrl', originUrl)
    }
    const loading = this.$loading({
      lock: true,
      text: 'Loading',
      spinner: 'el-icon-loading',
      background: 'rgba(0, 0, 0, 0.7)'
    });
    await this.getAccessToken()
    await this.getUserInfo()
    await this.getUserDetail()
    await this.getUserAuth()
    await this.getCompanyByDomain()

    loading.close()
    this.$router.push({
      path: '/' + redirectPath
    })
  }
}
</script>
<style scoped lang='scss'>
</style>